package com.ColonyCount.View;

import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.View;
import com.ColonyCount.Image.ImageRegion;

public class RegionView extends View {
	
	Bitmap sourceBitmap;
    List<ImageRegion> regions;
    int minRegionSize;
    
	public RegionView(Context context) 
	{
		super(context);
	}
	
	public RegionView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

	public void setSourceBitmap(Bitmap sourceBitmap)
	{
		this.sourceBitmap = sourceBitmap;
		invalidate();
	}

	public void setRegions(List<ImageRegion> regions)
	{
		this.regions = regions;
	}
	
	public void setMinRegionSize(int iMinSize)
	{
		this.minRegionSize = iMinSize;
	}
	
	@Override
	protected void onDraw (Canvas canvas)
	{
		super.onDraw(canvas);
		
		if(sourceBitmap == null)
			return;
		
    	float widthScale = (float)getWidth()/(float)sourceBitmap.getWidth();
    	float heightScale = (float)getHeight()/(float)sourceBitmap.getHeight();
    	float scale = Math.min(widthScale, heightScale);
    	canvas.scale(scale, scale);
    	
    	
    	Paint paint = new Paint();
    	paint.setAntiAlias(false);
    	paint.setFilterBitmap(false);
    	paint.setDither(false);
    	canvas.drawBitmap(sourceBitmap, 0, 0, paint);
    	
		Random rnd = new Random();
    	paint.setStrokeWidth(1);
		paint.setColor(Color.GREEN);

    	for(ImageRegion region : regions)
		{
    		if(region.getSize() >= minRegionSize)
    		{
            	paint.setColor(region.getColor());
    			Point[] points = region.getPoints();
    			for(int i=0;i<points.length;i++)
    			{
       				canvas.drawPoint(points[i].x, points[i].y , paint);
    			}
    		}
		}
	}
}
